package com.example.day1201_wlan.slice;

import com.example.day1201_wlan.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.wifi.*;

import java.util.List;
import java.util.Optional;

public class BasisFunction extends AbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "BasisFunction.class");

    private Text mText;
    EventHandler eventHandler = new EventHandler(EventRunner.current());

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_layout_ability_function);


        initCompent();
    }

    private void initCompent() {

        Button scanBtn = findComponentById(ResourceTable.Id_scan_button);
        scanBtn.setClickedListener(this::scanHot);

        Button infoBtn = findComponentById(ResourceTable.Id_get_info_button);
        infoBtn.setClickedListener(this::getHotInf);

        Button featureBtn = findComponentById(ResourceTable.Id_support_feature_button);
        featureBtn.setClickedListener(com -> functionFeature());

        mText = findComponentById(ResourceTable.Id_show_text);


    }

    /**
     * 支持那些功能
     */
    private void functionFeature() {
        WifiDevice wifiDevice = WifiDevice.getInstance(this);
        //是否支持2.4频段的WIFI
        boolean isSupportInfra = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_INFRA);
        //是否支持5G频段WIFI
        boolean isSupport5G = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_INFRA_5G);
        //是否支持p2p
        boolean isSupportP2P = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_P2P);
        //是否支持热点
        boolean isSupportHotSpot = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_MOBILE_HOTSPOT);

        mText.append("");
        mText.append(isSupportInfra ? "支持普通wifi\n" : "不支持普通wifi\n");
        mText.append(isSupport5G ? "支持5G wifi\n" : "不支持5G wifi\n");
        mText.append(isSupportP2P ? "支持P2P\n" : "不支持P2P\n");
        mText.append(isSupportHotSpot ? "支持移动热点\n" : "不支持移动热点\n");

    }

    /**
     * 获取热点信息
     */
    private void getHotInf(Component component) {
        WifiDevice wifiDevice = WifiDevice.getInstance(this);
        boolean connected = wifiDevice.isConnected();
        if (!connected) {
            new ToastDialog(getContext()).setText("未连接").show();
            return;
        }

        // 连接信息
        Optional<WifiLinkedInfo> linkedInfo = wifiDevice.getLinkedInfo();
        // wifi名称 ssid
        String ssid = linkedInfo.get().getSsid();
        // 国家码
//        String countryCode = wifiDevice.getCountryCode();
        // ip 信息
        Optional<IpInfo> ipInfo = wifiDevice.getIpInfo();
        int ipAddress = ipInfo.get().getIpAddress();
        int gateway = ipInfo.get().getGateway();

        mText.setText("");
//        mText.append("国家码: " + countryCode + System.lineSeparator());
        mText.append("SSID（名称）: " + ssid + System.lineSeparator());
        mText.append("IP: " + getLocationIpAddress(ipAddress) + System.lineSeparator());
        mText.append("Gateway（网关地址）: " + getLocationIpAddress(gateway));

    }

    /**
     * 将整数的ip转为xxx.xxx.xxx.xxx的形式
     * 原理是将二进制换算为10进制
     *
     * @param ip
     * @return xxx.xxx.xxx.xxx形式的IP
     */
    private String getLocationIpAddress(int ip) {
        String s = (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + (ip >> 24 & 0xFF);
        return s;
    }


    /**
     * 扫描热点
     */
    private void scanHot(Component component) {
        // WLAN管理对象
        WifiDevice wifiDevice = WifiDevice.getInstance(this);

        showScanResult(wifiDevice);

    }

    /**
     * 显示扫描结果
     *
     * @param wifiDevice WLAN管理对象
     */
    private void showScanResult(WifiDevice wifiDevice) {
        //发起扫描，得到发起扫描结果
        boolean isScanSuccess = wifiDevice.scan();
        HiLog.info(LABEL_LOG, "扫描：", isScanSuccess);

        if (isScanSuccess) {
            //获取结果信息列表
            List<WifiScanInfo> scanInfos = wifiDevice.getScanInfoList();
            mText.setText("");
            for (WifiScanInfo wifiScanInfo : scanInfos) {
                mText.append(wifiScanInfo.getSsid() + System.lineSeparator());
            }
        }
    }


    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onBackground() {
        super.onBackground();
        if (mText != null) {
            mText.setText(null);
        }
    }
}
